﻿Public Class Frm_massarif
    Public dv As Dossier
    Public avo As Depences

    Public Sub SumDepences()
        txt_total.Clear()
        Try
            Dim s As Double = 0
            For Each d As Dossier In Partager.moi.List_info_fichier
                'If d.Ref = Comb_marji3ona.Text Then
                '    dv = d
                '    dv.remplir_List_depences()
                'End If
            Next

            For Each khib As Depences In dv.List_depences
                s = s + khib.Montant_paye
            Next
            txt_total.Text = s
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
     
        If Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Me.Enabled = False
            Frm_Rech_massarif.Show()
        End If
    End Sub

    Private Sub Frm_massarif_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.close()
        Try
            Partager.nonInitial = New ArrayList
            Partager.nonInitial.Add("Comb_marji3ona")
            Partager.remplirDossier(Comb_marji3ona)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices

                avo = dv.List_depences(i)
                txt_mo2ada.Text = avo.Montant_paye
                Date_ada2.Value = avo.Date_payement
                txt_type.Text = avo.Type
                txt_num_wasl.Text = avo.Num_recu
                txt_molahadat.Text = avo.Remarque

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        If List_tous.SelectedIndices.Count < 1 Then
            Partager.vider(Me, Partager.nonInitial)
        End If
        SumDepences()
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        If (List_tous.SelectedIndices.Count > 0) Then
            MsgBox("عفوا لا يمكنك الإضافة، لإضافة مصاريف جديدة عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
        ElseIf Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Try
                If txt_mo2ada.Text <> "" And txt_type.Text <> "" Then

                    If MsgBox("هل تريد فعلا إضافة هذه المصاريف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة مصاريف") = MsgBoxResult.Yes Then
                        Partager.close()
                        Dim cli As New Depences(1, txt_mo2ada.Text, Date_ada2.Value, txt_type.Text, txt_num_wasl.Text, txt_molahadat.Text)
                        dv.Ajouter_depences(cli)

                        List_tous.Items.Clear()
                        For Each khib As Depences In dv.List_depences
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Montant_paye, khib.Date_payement, khib.Type, khib.Num_recu, khib.Remarque}))
                        Next
                        MsgBox("تمت الإضافة بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me, Partager.nonInitial)
                        SumDepences()
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If (List_tous.SelectedIndices.Count <= 0) Then
            MsgBox("عفوا لا يمكنك التعديل، اختر مصاريف من القائمة", MsgBoxStyle.Critical, "خطأ في التعديل")
        ElseIf Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في التعديل")
        Else
            Try
                If MsgBox("هل تريد فعلا تعديل هذه المصاريف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل إجراء") = MsgBoxResult.Yes Then
                    Partager.close()
                    Dim cli As New Depences(1, txt_mo2ada.Text, Date_ada2.Value, txt_type.Text, txt_num_wasl.Text, txt_molahadat.Text)
                    dv.modifier_depences(cli, avo.Id)

                    List_tous.Items.Clear()
                    For Each khib As Depences In dv.List_depences
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Montant_paye, khib.Date_payement, khib.Type, khib.Num_recu, khib.Remarque}))
                    Next

                    MsgBox("تمت تعديل المصاريف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me, Partager.nonInitial)
                    SumDepences()
                End If
        Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذه المصاريف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف مصاريف") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As Depences = dv.List_depences(List_tous.SelectedIndices.Item(0))
                    dv.suprimmer_depences(avo.Id)

                    List_tous.Items.Clear()
                    For Each khib As Depences In dv.List_depences
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Montant_paye, khib.Date_payement, khib.Type, khib.Num_recu, khib.Remarque}))
                    Next
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me, Partager.nonInitial)
                    SumDepences()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر مصاريف من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub


    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me, Partager.nonInitial)
        txt_mo2ada.Select()
        SumDepences()
    End Sub

    Private Sub txt_mo2ada_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mo2ada.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Partager.vider(Me, Partager.nonInitial)
        Try
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    dv = d
                    dv.remplir_List_depences()
                End If
            Next

            List_tous.Items.Clear()
            For Each khib As Depences In dv.List_depences
                List_tous.Items.Add(New ListViewItem(New String() {khib.Montant_paye, khib.Date_payement, khib.Type, khib.Num_recu, khib.Remarque}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        '------------------------------------------
        SumDepences()
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
    End Sub

End Class